package com.jinlejia.erp.ui.customer;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.freedom.yefeng.yfrecyclerview.YfListMode;
import com.freedom.yefeng.yfrecyclerview.YfLoadMoreListener;
import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.HouseFollowAdapter;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.FragmentCustomerFollowListBinding;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.Map;

import okhttp3.Call;

/**
 * A simple {@link Fragment} subclass.
 */
public class CustomerFollowListFragment extends Fragment implements YfLoadMoreListener{

    private View view;
    HouseFollowAdapter adapter;
    private int pageIndex = 1;
    private boolean mLoadingLock = false;
    String code,type;
        public CustomerFollowListFragment() {
    }


    public static CustomerFollowListFragment newInstance(String code ,String type) {
        CustomerFollowListFragment fragment = new CustomerFollowListFragment();
        Bundle args = new Bundle();
        args.putString("code", code);
        args.putString("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            code = getArguments().getString("code");
            type = getArguments().getString("type");
        }
    }

    private FragmentCustomerFollowListBinding binding;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_customer_follow_list, null);
            binding = DataBindingUtil.bind(view);
            initView();
        }
        // 缓存的rootView需要判断是否已经被加过parent，
        // 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }

        return view;
    }
    private void initView(){
        binding.yfRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.yfRecycler.setHasFixedSize(true);
        ArrayList<Map<String, Object>> data = new ArrayList();
        adapter = new HouseFollowAdapter(data);
        binding.yfRecycler.setAdapter(adapter);
        adapter.changeMode(YfListMode.MODE_LOADING);
        getData();
    }

    private void getData() {
        OkHttpUtils.get()
                .url(String.format(ApiConfig.CUSTOMER_FOLLOW_LIST, code, pageIndex,type))
                .build().execute(new MapCallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Map<String, Object> response, int id) {
                if (DataFilter.filter(getActivity(), response, null)) {
                    JSONArray data = JSON.parseArray(response.get("data").toString());
                    //数据列表
                    ArrayList<Map<String, Object>> list = JSON.parseObject(data.toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                    });
                    if (pageIndex == 1) {
                        adapter.setDataList(list);
                    } else {
                        adapter.addDataList(list);
                    }
                } else if (response.get("code").toString().equals("303")) {
                    //没有更多
                    pageIndex--;
                }
                mLoadingLock = false;
            }
        });
    }

    @Override
    public void loadMore() {
        if (adapter.getData().size() % 20 == 0) {
            if (mLoadingLock) {
                return;
            }
            mLoadingLock = true;
            pageIndex++;
            adapter.addFooter("loading");

            getData();
        }else if( adapter.getData().size() > 10 ){
            adapter.addFooter(R.string.is_all);
        }
    }
}
